package mk.gamemap.view.sprite
{
	import flash.display.Sprite;
	import flash.geom.Matrix;
	
	import game.tileMap.MapConst;
	import game.tileMap.SpriteCache;
	import game.tileMap.model.vo.BossUnit;
	import game.tileMap.model.vo.Character;
	import game.tileMap.model.vo.MapObj;
	
	import mk.gamemap.model.TileMapModel0;
	import mk.gamemap.view.AnimeFactory;
	import mk.gamemap.view.anime.AnimeClip;
	
	import mx.messaging.channels.AMFChannel;

	public class CharView extends SpriteView
	{
		
		
		public var moveSpeed:int=4;
		
		public var char:Character
		public var animeClip:AnimeClip;
		private var animeFactory:AnimeFactory
		public  var size:int=MapConst.TileSize;
		public function CharView(model:Character)
		{
			super(model);
			char=model;
			animeFactory=new AnimeFactory();
			if (char is BossUnit){
				animeClip=animeFactory.produceBoss();
			}else{
				animeClip=animeFactory.produceAnime();
				animeClip.displayObj.x=4
				animeClip.displayObj.y=4
			}
			sp=new Sprite();
			
//			//var matrix:Matrix = new Matrix();//建立matrix
//			var matrix:Matrix=animeClip.displayObj.transform.matrix;
//			matrix.tx -=16 ;
//			matrix.ty -=16;
//			//matrix.scale(3,3);
//			animeClip.displayObj.transform.matrix = matrix;//使用matrix
			sp.addChild(animeClip.displayObj);
			X=char.x
			Y=char.y
			sp.x=char.x*MapConst.TileSize;
			sp.y=char.y*MapConst.TileSize;
				
		}
		
		public function get moving():Boolean{
	//		trace(sp.y +":::"+Y * size );
			return (sp.x != X *  size|| sp.y != Y * size)
		}
		
		public function get canMove():Boolean{
			return !moving
		}
		
		
		
		override public function  update():void{
			if (!moving){
				char.update();
			}
			
					animeClip.nextFrame();
			
			
			
			updateMove()
			
			
		}
		
		public function  updateMove():void{
			var distance:int = moveSpeed
				//lastMoving=moving;
			//distance *= 2 if dash?     
			if (X * size < sp.x){
				sp.x = Math.max(sp.x - distance, X * size); 
			}
			
			if (X * size > sp.x){
				sp.x = Math.min(sp.x + distance, X * size) 
			}
			if (Y * size < sp.y){
				sp.y =  Math.max(sp.y - distance, Y * size); 
			}
			if (Y * size> sp.y){
				sp.y = Math.min(sp.y + distance, Y * size) 
			}
			//update_bush_depth unless moving?
			/*if @walk_anime
				@anime_count += 1.5
			elsif @step_anime
				@anime_count += 1
			end*/
			//if (lastMoving
		}
		
		public function passable(X:int, Y:int):Boolean{
			return char.passable(X,Y);    
		}
			
		
		public function  moveUp():void{
			if (!canMove){
				return;
			}
			if (passable(X, Y-1)) {                
				//turn_down
				Y = (Y-1)
				sp.y = (Y+1)*size
			
				//@move_failed = false
			}else {
				
				char.check_event_trigger_touch(X, Y-1)
			}
		}
		public function  moveDown():void{
			if (!canMove){
				return;
			}
			if (passable(X, Y+1)) {                
				//turn_down
				Y = (Y+1)
				sp.y = (Y-1)*size
				
				//@move_failed = false
			}else{
				char.check_event_trigger_touch(X, Y+1)
			}
		}
		public function  moveLeft():void{
			if (!canMove){
				return;
			}
			if (passable(X-1, Y)) {                
				//turn_down
				X = (X-1)
				sp.x = (X+1)*size
				
				//@move_failed = false
			}else{
				char.check_event_trigger_touch(X-1, Y)
			}
		}
		public function  moveRight():void{
			if (!canMove){
				return;
			}
			if (passable(X+1, Y)) {                
				
				sp.x = X*size
				X = X+1
				
				
				//@move_failed = false
			}else{
				char.check_event_trigger_touch(X+1, Y)
			}
		}
	}
}